from django.shortcuts import render
from .tools import *


# 实验1的首页
def experiment2(request):
    return render(request, 'search_app/experiment2.html')


# 全局变量数组
arr = []
status = 0


# 1
def random_generation(request):
    global arr
    if request.method == 'POST':
        n = int(request.POST.get('n', 0))
        method = request.POST.get('method', None)

        # 默认生成随机状态的数组
        states = ['未排序', '升序', '降序', '先升后降', '先降后升']
        method_index = random.randint(0, 4) if method == 'random' else int(method)
        arr = generate_array(n, method_index)

        return render(request, 'search_app/1.html', {'arr': arr, 'n': n, 'method': states[method_index]})

    return render(request, 'search_app/1.html')


# 2
def status_check(request):
    global status
    if request.method == 'POST':
        status = check_array_order(arr)
        return render(request, 'search_app/2.html', {'result': status})
    return render(request, 'search_app/2.html')


# 3
def order_statistics(request):
    if request.method == 'POST':
        target = int(request.POST.get('target'))
        res, cnt = sequential_search(arr, target)
        result = {
            'res': res,
            'cnt': cnt
        }
        return render(request, 'search_app/3.html', {'result': result, 'arr': arr})
    return render(request, 'search_app/3.html', {'arr': arr})


# 4
def multiple_statistics(request):
    if request.method == 'POST':
        result = {}
        res = []
        # 获取目标值
        target = int(request.POST.get('target'))
        # 获取查找方式
        method = int(request.POST.get('method', 0))
        methods = ["顺序查找", "二分查找", "三分查找", "插值查找"]
        if method == 0:
            res = linear_search(arr, target)
        elif method == 1:
            res = binary_search(arr, target)
        elif method == 2:
            res = ternary_search(arr, target)
        elif method == 3:
            res = interpolation_search(arr, target)
        if len(res) == 2:
            result = {
                'info': res[1]
            }
        elif len(res) == 3:
            index, comparison_count = res[1], res[2]
            result = {
                'index': index,
                'cnt': comparison_count
            }
        elif len(res) == 4:
            index, item, comparison_count = res[1], res[2], res[3]
            result = {
                'index': index,
                'item': item,
                'cnt': comparison_count
            }
        return render(request, 'search_app/4.html', {'result': result, 'target': target, 'arr': arr, 'method': methods[method]})
    return render(request, 'search_app/4.html', {'arr': arr})


# 5
def binary_ternary(request):
    if request.method == 'POST':
        status = check_array_order(arr)
        if status == 3:
            res = "大"
            m1, cnt1 = find_peak_binary(arr)
            m2, cnt2 = find_peak_ternary(arr)
            result = {
                'm1': m1,
                'cnt1': cnt1,
                'm2': m2,
                'cnt2': cnt2
            }
        elif status == 4:
            res = "小"
            m1, cnt1 = find_min_binary(arr)
            m2, cnt2 = find_min_ternary(arr)
            result = {
                'm1': m1,
                'cnt1': cnt1,
                'm2': m2,
                'cnt2': cnt2
            }
        else:
            res = ''
            result = {
                'info': "生成的数组既不是先升后降也不是先降后升，请重新生成数组"
            }
        return render(request, 'search_app/5.html', {'result': result, 'arr': arr, 'res': res})
    return render(request, 'search_app/5.html', {'arr': arr})


# 6
def unordered_multiple(request):
    if request.method == 'POST':
        k = int(request.POST.get('k'))
        status = check_array_order(arr)
        if status == 0:
            m1, cnt1 = kth_smallest_brute_force(arr, k)
            m2, cnt2 = kth_smallest_sorted(arr, k)
            m3, cnt3 = kth_smallest_quickselect(arr, k)
            result = {
                'm1': m1,
                'cnt1': cnt1,
                'm2': m2,
                'cnt2': cnt2,
                'm3': m3,
                'cnt3': cnt3
            }
        else:
            result = {
                'info': "生成的数组不是无序数组，请重新生成数组"
            }
        return render(request, 'search_app/6.html', {'result': result, 'arr': arr, 'k': k})
    return render(request, 'search_app/6.html', {'arr': arr})
